package com.ai.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ShortUrlFilter implements Filter {
    
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        String requestURI = httpRequest.getRequestURI();
        if("/".equals(requestURI) || requestURI.contains(".action")) {
            filterChain.doFilter(request, response);
        } else {
            //跳转至controller中
            httpRequest.setAttribute("paramStr", requestURI.substring(1, requestURI.length()));
            httpRequest.getRequestDispatcher("htmlPageRequest.action").forward(httpRequest, httpResponse);
        }
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
    }

}
